package leetcode;

/**
 * 罗马数字转整数  
 */
public class LeetCode13 {
    public static char[] luoma = new char[]{'I', 'V', 'X', 'L', 'C', 'D', 'M'};
    public static int[] value = new int[]{1, 5, 10, 50, 100, 500, 1000};


    public int romanToInt(String s) {
        int result = 0;
        for (int i = 0; i < s.length() - 1; i++) {
            int idx = getIndex(s.charAt(i));
            if (idx < 0) {
                return 0;
            }
            int nextIdx = getIndex(s.charAt(i + 1));
            if (value[idx] < value[nextIdx]) {
                result -= value[idx];
            } else {
                result += value[idx];
            }
        }
        result += value[getIndex(s.charAt(s.length() - 1))];
        return result;
    }

    public int getIndex(char s) {
        for (int i = 0; i < luoma.length; i++) {
            if (luoma[i] == (s)) {
                return i;
            }
        }
        return -1;
    }
    public static void main(String[] args) {
        LeetCode13 leetCode13 = new LeetCode13();
        System.out.println(leetCode13.romanToInt("IV"));
    }

}
